package kr.co.skplanet.commute.service;

import java.util.List;

import kr.co.skplanet.commute.dao.MemberDAO;
import kr.co.skplanet.commute.dao.PointDAO;
import kr.co.skplanet.commute.dao.RouteDAO;
import kr.co.skplanet.commute.dao.StopInfoDAO;
import kr.co.skplanet.commute.domain.Member;
import kr.co.skplanet.commute.domain.Point;
import kr.co.skplanet.commute.domain.Route;
import kr.co.skplanet.commute.domain.StopInfo;
import kr.co.skplanet.commute.util.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service(value="memberService")
@Transactional 
public class MemberService {
	@Autowired
	MemberDAO memberDAO;
	
	@Transactional(readOnly = true)
	public List<Member> getMemberList() {
		List<Member> list = memberDAO.getAll();
		return list;
	}
	@Transactional(readOnly = true)
	public Member getMember(Long id) {
		return memberDAO.findMember(id);
	}
	@Transactional(readOnly = true)
	public Member getMember(String email) {
		return memberDAO.findMember(email);
	}
	@Transactional(readOnly = true)
	public Member getMember(String email, String passwd) {
		return memberDAO.findMember(email, passwd);
	}
	@Transactional(readOnly = true)
	public Member getMemberByAuthKey(String authkey) {
		return memberDAO.findMemberByAuthKey(authkey);
	}
	
	@Transactional
	public Member saveMember(Member member) {
		if(member.getSalt() == null)
			member.setSalt(StringUtils.genRandomStringWithSpecialCharacter(20));
		
		member.setAuthKey(StringUtils.genSHA256(member.getEmail(), member.getSalt(), 1000));
		member.setEmail(StringUtils.genSHA256(member.getEmail()));
		member.setPassword(StringUtils.genSHA256(member.getPassword(), member.getSalt(), 1000));
		
		memberDAO.save(member);
		return member;
	}
}	// MemberService
